﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Tunynet.MobileSMS
{
    /// <summary>
    /// 客户端短信发送调用类
    /// 新增云信平台发送，新增如下方法：
    /// 1，YunXinSendMessage发送短信
    /// 2，YunXinSendMessage2采用service形式发送短信
    /// 3，YunXinGetBalance获取当前余额
    /// 4，YunXinGetReport获取短信发送后的状态
    /// 5，YunXinGetMo获取发送后的回复
    /// 6，YunXinGetGetMoExt获取指定批号的回复
    /// 
    /// </summary>
    public class TopenSmsSend
    {
        static XmlResponse xmlresponse = new XmlResponse();

        private static string _userName = "fzfj";
        private static string _password = "lu5213777";

        /// <summary>
        /// 返回发送短信的结果
        /// </summary>
        /// <param name="result"></param>
        /// <returns></returns>
        private static SMSPlatformSendResult SendResults(SMSPlatformSendResult result)
        {
            switch (result.Description)
            {
                case "-1":
                    result.Description = "提交接口错误";
                    result.IsSuccess = false;
                    break;
                case "-3":
                    result.Description = "用户名或密码错误";
                    result.IsSuccess = false;
                    break;
                case "-4":
                    result.Description = "短信内容和备案模板不一样";
                    result.IsSuccess = false;
                    break;
                case "-5":
                    result.Description = "签名不正确";
                    result.IsSuccess = false;
                    break;
                case "-7":
                    result.Description = "余额不足";
                    result.IsSuccess = false;
                    break;
                case "-8":
                    result.Description = "通道错误";
                    result.IsSuccess = false;
                    break;
                case "-9":
                    result.Description = "无效号码";
                    result.IsSuccess = false;
                    break;
                default:
                    result.IsSuccess = true;
                    result.Description = "发送成功";
                    break;
            }
            return result;
        }

        /// <summary>
        /// 【使用云信发送】发送系统短信
        /// </summary>
        /// <param name="msgContent">短信内容,短信内容必须以签名结尾,签名格式为：【签名内容】</param>
        /// <param name="mobileNumbers">需要发送的手机号码</param>
        /// <returns></returns>
        public static SMSPlatformSendResult YunXinSendMessage(string msgContent, string mobileNumbers)
        {
            var userCode = YunXinParam.Create("userCode", _userName);
            var userPass = YunXinParam.Create("userPass", _password);
            var DesNo = YunXinParam.Create("DesNo", mobileNumbers);
            var Msg = YunXinParam.Create("Msg", msgContent);
            var Channel = YunXinParam.Create("Channel", "0");
            var str = xmlresponse.GetResponse<System.Xml.XmlDocument>(YunXinMethods.SendMsg.ToString(), userCode, userPass, DesNo, Msg, Channel);
            SMSPlatformSendResult result = new SMSPlatformSendResult();
            result.Description = str.DocumentElement.InnerText;
            result = SendResults(result);
            return result;


        }

         //<summary>
         //【使用云信发送,采用服务的形式】发送系统短信
         //</summary>
         //<param name="msgContent">短信内容,短信内容必须以签名结尾,签名格式为：【签名内容】</param>
         //<param name="mobileNumbers">需要发送的手机号码(多号码以逗号分隔)</param>
         //<returns></returns>
        public static SMSPlatformSendResult YunXinSendMessage2(string msgContent, string mobileNumbers)
        {
            ServiceReference1.MsgSendSoapClient topen = new ServiceReference1.MsgSendSoapClient();
            string sendResult = topen.SendMsg(_userName, _password, mobileNumbers, msgContent, "0");
            SMSPlatformSendResult result = new SMSPlatformSendResult();
            result.Description = sendResult;
            result = SendResults(result);
            return result;
        }


        /// <summary>
        /// 【使用云信发送】获取当前账户余额
        /// </summary>
        /// <returns></returns>
        public static SMSPlatformSendResult YunXinGetBalance()
        {
            var userCode = YunXinParam.Create("userCode", _userName);
            var userPass = YunXinParam.Create("userPass", _password);
            var str = xmlresponse.GetResponse<System.Xml.XmlDocument>(YunXinMethods.GetBalance.ToString(), userCode, userPass);
            SMSPlatformSendResult result = new SMSPlatformSendResult();
            result.Description = str.GetElementsByTagName("string")[0].InnerText;
            return result;
        }

        /// <summary>
        /// 获取短信发送后的发送状态
        /// </summary>
        /// <returns></returns>
        public static SMSPlatformSendResult YunXinGetReport()
        {
            var userCode = YunXinParam.Create("userCode", _userName);
            var userPass = YunXinParam.Create("userPass", _password);
            var str = xmlresponse.GetResponse<System.Xml.XmlDocument>(YunXinMethods.GetReport.ToString(), userCode, userPass);
            SMSPlatformSendResult result = new SMSPlatformSendResult();
            result.Description = str.GetElementsByTagName("string")[0].InnerText;
            return result;
        }

        /// <summary>
        /// 获取短信发送后的回复
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="password">密码</param>
        /// <returns></returns>
        public static SMSPlatformSendResult YunXinGetMo()
        {
            var userCode = YunXinParam.Create("userCode", _userName);
            var userPass = YunXinParam.Create("userPass", _password);
            var str = xmlresponse.GetResponse<System.Xml.XmlDocument>(YunXinMethods.GetMo.ToString(), userCode, userPass);
            SMSPlatformSendResult result = new SMSPlatformSendResult();
            result.Description = str.GetElementsByTagName("string")[0].InnerText;

            return result;
        }

        /// <summary>
        /// 获取短信发送后指定批次号的回复
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="password">密码</param>
        /// <param name="minID">批次号</param>
        /// <returns></returns>
        public static SMSPlatformSendResult YunXinGetGetMoExt(string minID)
        {
            var userCode = YunXinParam.Create("userCode", _userName);
            var userPass = YunXinParam.Create("userPass", _password);
            var minId = YunXinParam.Create("minId", minID);
            var str = xmlresponse.GetResponse<System.Xml.XmlDocument>(YunXinMethods.GetMoExt.ToString(), userCode, userPass, minId);
            SMSPlatformSendResult result = new SMSPlatformSendResult();
            result.Description = str.GetElementsByTagName("string")[0].InnerText;

            return result;
        }
    }
}
